﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Infancy.Core.Entities;
using Infancy.Core.Validation;
using System.Web.Mvc;

namespace Infancy.Mvc
{
    public static class ValidateExtensions
    {
        public static ValidatedResult ModelValidate<TEntity>(this ValidateEntityBase<TEntity> entity, ModelStateDictionary modelStates)
            where TEntity : IEntity
        {
            ValidatedResult result = entity.Validate();
            if (!result.Result)
            {
                foreach (var item in result.Errors)
                {
                    modelStates.AddModelError(item.Key, item.Value);
                }
            }
            return result;
        }
    }
}
